jQuery(document).ready(function(){

	$j("#progress #pmessage").text("Please wait... system is loading general page managment system...");
	$j("#progress").fadeIn("slow");
	
	$j.ajax({
		type: "POST",
		url:  "http://localhost/engine/backend/pages/manage/general",
		data: "page"+page+"&id="+id,
		dataType: "json",
		error: function (XMLHttpRequest, textStatus, errorThrown) {
			$j("#progress").addClass("progress-error");
			$j("#pmessage").text( "We can't open page. There is some problem with the script!" );
			setTimeout ( '$j("#progress").fadeOut("normal");', 10000 );
		},
		success: function ( data, status ){
			$j("#progress").removeClass("progress-error");
			$j("#pmessage").text( data.message );
			setTimeout ( '$j("#progress").fadeOut("normal");', 2000 );
		}
	});
	
	$j("#next01").bind("click", function(){
		$j("#progress").fadeOut("fast");
		$j("#progress #pmessage").text("Please wait... Trying to validate data...");
		$j("#progress").fadeIn("slow");
		
		$j.ajax({
			type: "POST",
			url:  "http://localhost/engine/backend/pages/manage/general-validation",
			data: "page"+page+"&id="+id,
			dataType: "json",
			error: function (XMLHttpRequest, textStatus, errorThrown) {
				$j("#progress").addClass("progress-error");
				$j("#pmessage").text( "We can't open page. There is some problem with the script!" );
				setTimeout ( '$j("#progress").fadeOut("normal");', 10000 );
			},
			success: function ( data, status ){
				
				if ( data.status == "true" )
				{
					$j("#progress").removeClass("progress-error");
					$j("#progress #pmessage").text( data.message );
					
					
					$j.ajax({
						type: "POST",
						url:  "http://localhost/engine/backend/pages/manage/permissions",
						data: "page"+page+"&id="+id,
						dataType: "json",
						error: function (XMLHttpRequest, textStatus, errorThrown) {
							$j("#progress").addClass("progress-error");
							$j("#pmessage").text( "We can't open page. There is some problem with the script!" );
							setTimeout ( '$j("#progress").fadeOut("normal");', 10000 );
						},
						success: function ( data, status ){
							$j("#progress").removeClass("progress-error");
							$j("#next01").fadeOut("fast");
							$j("#t_one").addClass("red");
							$j("#t_two").removeClass("blue");
							$j("#t_two").addClass("current");
							$j("#next02").css({
								display: "inline-block"
							});
							$j("#pmessage").text( data.message );
							setTimeout ( '$j("#progress").fadeOut("normal");', 2000 );
							$j("#tabs").fadeIn("slow", function(){
								document.getElementById("tabs").innerHTML = data.content;
							});
						}
					});
					
				}
				else
				{
					$j("#progress").addClass("progress-error");
					$j("#pmessage").text( data.message );
					$j("#"+data.input).css({
						backgroundColor: "#b70a33",
						color: "#fff",
						border: "1px #d80e3d solid"
					});

					$j("#validmessage").css({
						fontSize: "14px",
						color:"#b70a33"
					});
					
					$j("#validmessage").text(data.inputmessage);
					setTimeout ( '$j("#progress").fadeOut("normal");', 10000 );	
				}
			}
		});
	});
	
	$j("#publish").bind("click", function(){
		$j("#progress #pmessage").text("Please wait... system is preparing data for publishing...");
		$j("#progress").fadeIn("slow");
		
		
	});

	$j("#discard").bind("click", function(){
		
		var conf = confirm ( "Do you really want to discard current modifications?" );
		if ( conf )
		{
			$j("#progress #pmessage").text("Please wait...");
			$j("#progress").fadeIn("slow");
			setTimeout ( 'document.location="http://localhost/engine/backend/pages"', 3000 );	
		}
		else
		{
			$j("#progress #pmessage").text("Canceled...");
			$j("#progress").fadeIn("slow");
			setTimeout ( '$j("#progress").fadeOut("normal");', 3000 );	
		}
		
	});	
});